package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.entity.bo.HouseQueryBo;
import com.atguigu.entity.vo.HouseVo;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:hx
 * @DateTime:2022/9/29 20:06
 * @Description:
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private UserFollowService userFollowService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @RequestBody HouseQueryBo houseQueryBo){
        PageInfo<HouseVo> page = houseService.findHousePage(pageNum,pageSize,houseQueryBo);
        return Result.ok(page);
    }

    @RequestMapping("/info/{houseId}")
    public Result info(@PathVariable Long houseId, HttpSession session){
        House house = houseService.getById(houseId);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(houseId);
        List<HouseImage> houseImage1List = houseImageService.findHouseImageListByHouseIdAndType(houseId, 1);

        HashMap<String, Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);

        UserInfo userInfo = (UserInfo) session.getAttribute("USER");
        boolean isFollow = false;
        if (userInfo != null){
            isFollow = userFollowService.getFollowByUserIdAndHouseId(userInfo.getId(),houseId);
        }

        map.put("isFollow",isFollow);

        return Result.ok(map);
    }
}
